package com.zxy.salenet.web.web.controller;

import com.zxy.salenet.web.pojo.SalenetAdmins;
import com.zxy.salenet.web.pojo.SalenetUser;
import com.zxy.salenet.web.service.LoginService;
import com.zxy.salenet.web.utils.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author: zxy
 * @date: 2021/3/1 / 10:18
 * @version: 1.0
 * @description:   用户注册|用户登陆界面处理
 */
  /**
 * LoginController.
 */
@Api(value = "登陆|注册|注销 API",tags = "登陆|注册|注销 API")
@RestController
@RequestMapping(path = "/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 用户注册
     *
     * @return
     */
    @ApiOperation(value = "用户注册 发送用户对象 id 为空 ",notes = "用户注册  数据JSON {\n" +
            "    \"userName\":\"买辣椒也用券\",\n" +
            "    \"userRealName\":\"张泉\",\n" +
            "    \"userSex\":\"男\",\n" +
            "    \"userAge\":25,\n" +
            "    \"userAddress\":\"河南省郑州市二七区金碧苑4401\",\n" +
            "    \"userBrith\":\"1998-02-02\",\n" +
            "    \"userPasswd\":\"123456..\",\n" +
            "    \"userPhone\":\"19076885677\",\n" +
            "    \"userEmail\":\"123@qq.com\",\n" +
            "    \"userPaypass\":\"123456\"\n" +
            "}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/userRegister")
    public Json userRegister(@RequestBody SalenetUser salenetUser){
        Json result = new Json();
        //调用用户添加
        System.out.println(salenetUser.toString());
        Integer insertResult = loginService.addUser(salenetUser);
        //如果添加失败
        if(insertResult == 0) {
            result.setCode(300);
            result.setData(0);
            result.setMessage("注册失败");
            return result;
        }
        if(insertResult == 4){
            result.setCode(300);
            result.setData(0);
            result.setMessage("用户名重复");
            return result;
        }
        if(insertResult == 5){
            result.setCode(300);
            result.setData(0);
            result.setMessage("手机号重复");
            return result;
        }
        if(insertResult == 6){
            result.setCode(300);
            result.setData(0);
            result.setMessage("真实姓名重复");
            return result;
        }
        result.setCode(200);
        result.setData(insertResult);
        result.setMessage("注册成功");
        return result;
    }

    /**
     * 用户登陆
     *
     * @param session   session存储用户信息
     * @return  获取到的当前用户登陆信息
     */
    @ApiOperation(value = "用户登陆",notes = "用户登陆 用户名 密码 JSON格式 {\"userName\":\"13097814606\",\"userPasswd\":\"123456\"} 把登陆对象信息存储到前端")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/userLogin")
    private Json userLogin(@RequestBody SalenetUser user, HttpSession session){
        Json result = new Json();
        SalenetUser salenetUser = loginService.userLogin(user.getUserName(), user.getUserPasswd());
        if(salenetUser != null){
            //将数据存储到session中
            session.setAttribute("userToken",salenetUser);
            result.setData(salenetUser);
            result.setCode(200);
            result.setMessage("登陆成功!");
            return result;
        }
        result.setCode(600);
        result.setData(null);
        result.setMessage("账号或密码错误!");
        return result;
    }

    /**
     * 注销当前用户
     * @param session
     * @return
     */
    @ApiOperation(value = "用户注销操作",notes = "无需参数  直接请求")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = false)
    @PostMapping(path = "/logout")
    private Json userLogOut(HttpSession session){
        System.out.println("注销");
        Json jsonResult = new Json();
        //清空session中的信息
        session.invalidate();
        jsonResult.setCode(200);
        jsonResult.setMessage("注销成功");
        //返回登陆界面
        return jsonResult;
    }

    /**
     * 管理员登陆
     * @return
     */
    @ApiOperation(value = "管理员后台登陆",notes = "管理员后台登录  用户名 密码 JSON 格式 {\"adminsName\":\"wangfugui\",\"adminsPasswd\":\"password\"}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/adminLogin")
    private Json adminLogin(@RequestBody SalenetAdmins user, HttpSession session){
        Json json = new Json();
        SalenetAdmins salenetAdmins = loginService.adminsLogin(user.getAdminsName(), user.getAdminsPasswd());
        if(salenetAdmins != null){
            session.setAttribute("adminSession",salenetAdmins);
            json.setCode(200);
            json.setData(salenetAdmins);
            json.setMessage("登陆成功");
            return json;
        }
        json.setData(null);
        json.setMessage("账号密码错误,请重新输入");
        json.setCode(700);
        return json;
    }
}
